<template>
	<view class="container">
		<button class="wx" @click="wxLogin">微信用户一键登录</button>
		<button type="default" @click="getUserInfo">手机号一键登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
			}
		},
		methods: {
			wxLogin() {
				uni.getUserProfile({
					desc: '用于完善用户信息',
					success: result => {
						this.userInfo = result.userInfo;
						this.getCode();
					},
					fail: res => {
						console.log(res);
					},
				})
			},
			getCode() {
				uni.login({
					provider: 'weixin',
					success: ({
						code
					}) => {
						this.getToken(code);
					}
				})
			},
			async getToken(code) {
				const result = await this.$api.profile.login({
					code,
					userInfo: this.userInfo,
					appId: "wxd8b40bb48d78ec57",
					appSecret: "40a16687e8fd471a853ca8975a16cf9a"
				});
				uni.setStorage({
					key: "token",
					data: result.token
				});
				this.getUserInfo(result.token);
			},
			async getUserInfo(token) {
				await this.$api.profile.auth(token,this.userInfo);
				const result = await this.$api.profile.getUserInfo(token);
				getApp().globalData.userInfo = result[0];
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		width: 500rpx;
		margin: 50% auto;

		.wx {
			background: #4CD964;
			color: #FFFFFF;
		}
	}
</style>
